<template>
  <el-dialog title="产品编辑"  :close-on-click-modal="false" :visible.sync="show" class="dialog_index dialog_banner"
   :before-close="cancle">
    <el-form ref="addForm" v-loading="loading" class="article_manage_product_form" :rules="rules" :model="form">
      <el-form-item label="产品型号名称："  v-for="(item, index) in form.data" 
      :prop="'data.' + index + '.product_id'" :rules="rules.product_id"
      :key="item.product_id" :class="dataClass">
        <el-select   placeholder="产品型号名称（必填项）" filterable v-model="item.product_id"  >
          <el-option v-for="product in productModelNameList" :key="product.value" :value="product.product_id" :label="product.label"></el-option>
        </el-select> 
        <i class="iconfont  icon-delete_normal" @click="removeProduct(index)" v-show="dataClass !=='noDel'"></i>

      </el-form-item>

      <el-button type="search" @click="addProduct">添加产品</el-button>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="search" @click="submitForm">保存</el-button>
      <el-button @click="cancle">取消</el-button>
    </span>
  </el-dialog>
  
</template>

<script>
import { articleProductEditGet, articleProductEditPost } from '@/api/article/article_manage.js'
import { productModelNameList } from '@/api/product/productBasic.js'

export default {
  data () {
    return  {
      loading: false,
      form: {
        data: [
          { product_id: '', article_id: '', product_model_name: ''},
        ]
      },
      rules:{
        product_id:[
          { required: true, message: '字段产品型号名称为必填项', trigger: 'blur' },
        ],
      },
      productModelNameList: []
    }
  },
  props: {
    show: Boolean,
    closeDialog:Function,
    id: Array
  },
  computed: {
    dataClass:  function(){ // 判断当前数据长度控制样式显示
      return this.form.data.length == 1? 'noDel': ''
    }
  },
  watch: {
    show: function(){
      if(this.show){
        this.getProductModelName() 
        this.getArticleProductEdit()
      }
    }
  },
  methods: {
    getArticleProductEdit(){ // 获取编辑数据
      let params = {
        article_id: this.id
      }
      articleProductEditGet(params).then(res=>{
        this.form.data= res.data

      })
    },
    removeProduct (index) { //删除
      this.form.data.splice(index, 1)
    },
    addProduct (){ //  添加
      let obj = { product_id: '', article_id: '', product_model_name: ''}
      this.form.data.push(obj)
    },
    getProductModelName () { // 产品型号名称下拉框
      productModelNameList().then(res=>{
        if(res.state ==1){
          this.productModelNameList = res.data
        }
      })
    },

    postData () { //提交数据
      let list = JSON.parse(JSON.stringify(this.form.data))
      for (let i = 0; i < list.length; i++) { //封装对应的数据格式
        for (let j = 0; j < this.productModelNameList.length; j++) {
          if(list[i].product_id == this.productModelNameList[j].product_id){
            list[i].product_model_name = this.productModelNameList[j].value 
          }
        }
        list[i].article_id = this.id
      }

      let params  ={
        article_id:JSON.stringify(this.id),
        data: JSON.stringify(list)
      }
      articleProductEditPost(params).then(res=>{
        if(res.state == 1){
          this.$message.success('保存成功')   
          this.cancle()
        }
      })
    },
    submitForm () { // 校验
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.postData()
        }
      })
    },
    cancle(){
      // this.$refs.addSiteFrom.clearValidate();
      this.$refs.addForm.resetFields(); // 清空原来的数据
      this.$emit('closeDialog')

    },
  }
}
</script>

<style>

</style>